
uniform sampler2DRect fsm;
uniform sampler2DRect agentsPos;
uniform sampler2DRect soilLabels;

uniform float deltaTime;

void main ()
{
	vec4 agentLookUp = texture2DRect (agentsPos, gl_TexCoord[0].st);
	vec2 agentPos = agentLookUp.rg;
	float agentState = agentLookUp.b;
	float prevDir = agentLookUp.a;
  
	float soilLabelsLookUp = texture2DRect (soilLabels, agentPos).r;

	vec3 fsmOut = texture2DRect (fsm, vec2(soilLabelsLookUp.r, agentState)).rgb;

	vec2 deltaPos = vec2(fsmOut.r-1.0, fsmOut.g);
  
	// Check if size of delta (direction) is zero (1: zero, 0: nonzero)
	//float isDeltaValid = step(0.0, -length(deltaPos));
	
	// If zero, use old direction, else, use new direction
	float newDir = (atan(deltaPos.y, deltaPos.x));
   // float dir = mix(newDir,prevDir , isDeltaValid);

	gl_FragColor = vec4 (agentPos + deltaPos*deltaTime, fsmOut.b, newDir);
	
}